package ec.edu.ups.cidi.siia.plamdep.utils;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import ec.edu.ups.cidi.siia.plamdep.interfaces.ListenerLogguer;


public class Log {

		//
		private static final PrintStream salidaConsola = System.out;
		/**
		 * Activa o desactiva la salida por consola
		 * @param ban
		 */
		public static void enableSalidaConsola(boolean ban) {
			try {
				if (!ban) {
					System.setOut(new PrintStream(new PrintStream(
							new OutputStream() {
								public void write(int b) throws IOException {
								}
							})));
				} else {
					salidaConsola.flush();
					System.setOut(salidaConsola);
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
			
		////////////////////////////////////Utilidades Logger
			
			public static String LEVEL_ERROR="ERROR";
			public static String LEVEL_WARNING="WARNING";
			public static String LEVEL_MSG="MSG";
			public static String LEVEL_LOAD="LOAD";
			public static String LEVEL_SUCESS="SUCESS";
			
			public static String MOD_ANLS_LEXICO="ANALISIS_LEXICO";
			public static String MOD_WEBCRAWLER="WEBCRAWLER";
			public static String MOD_UTILS="UTILS";
			public static String MOD_EJECUTOR="EJECUTOR";
			public static String MOD_CARGA_JAR="CARGA_JAR";
			public static String MOD_CARGA_DOCUMENTOS="CARGA_DOCUMENTOS";
			public static String MOD_CONFIGURACION="CONFIGURACION";
			public static String MOD_GUI="GUI";
			public static String MOD_ALGORITMO="ALGORITMO";
			
			public static boolean ENABLE_LOGGIN_MOD_ANLS_LEXICO=true;
			public static boolean ENABLE_LOGGIN_MOD_WEBCRAWLER=true;
			public static boolean ENABLE_LOGGIN_MOD_UTILS=true;
			public static boolean ENABLE_LOGGIN_MOD_EJECUTOR=true;
			public static boolean ENABLE_LOGGIN_MOD_CARGA_JAR=true;
			public static boolean ENABLE_LOGGIN_MOD_CARGA_DOCUMENTOS=true;
			public static boolean ENABLE_LOGGIN_MOD_CONFIGURACION=true;
			public static boolean ENABLE_LOGGIN_MOD_GUI=true;
			public static boolean ENABLE_LOGGIN_MOD_ALGORITMO=true;
			
			public static boolean ENABLE_LOGGIN_LEVEL_ERROR=true;
			public static boolean ENABLE_LOGGIN_LEVEL_WARNING=true;
			public static boolean ENABLE_LOGGIN_LEVEL_MSG=true;
			public static boolean ENABLE_LOGGIN_LEVEL_LOAD=true;
			public static boolean ENABLE_LOGGIN_LEVEL_SUCESS=true;
			
			private static int hora, minutos, segundos;
			private static Calendar calendario = Calendar.getInstance();
			private static String horaMSG;
			
			public static List<ListenerLogguer> logguerListeners=new ArrayList<ListenerLogguer>();
			
			
			
			public static void log(String modulo,Class className,String msg)
			{
				log(null, modulo, LEVEL_MSG, className, msg);
				
			}
			public static void log(Class className,String msg)
			{
				log(null, MOD_UTILS, LEVEL_MSG, className, msg);
				
			}
			public static void log(Exception error,String modulo,String level,Class className,String msg)
			{
				
				if(!permitirLog(modulo, level))return;//verifica si el log esta permitido
				

				while(msg.startsWith("\n"))
				{	msg=msg.replaceFirst("\n", "");
					System.out.println("");
				}
				
				calendario = Calendar.getInstance();
				hora =calendario.get(Calendar.HOUR_OF_DAY);
				minutos = calendario.get(Calendar.MINUTE);
				segundos = calendario.get(Calendar.SECOND);
				horaMSG=hora + ":" + minutos + ":" + segundos;

				
				
				enableSalidaConsola(true);
				if(error!=null)error.printStackTrace();
				String output="["+horaMSG+"]["+modulo+"]-["+level+"]-["+className.getName().replaceAll("ec.edu.ups.cidi.siia.plamdep.", "")+"]==>"+msg;
				System.out.println(output);
				
				for(ListenerLogguer lg:logguerListeners)
				{
					lg.onLog(output);
					lg.onLog(error, modulo, level, className, msg);
				}	
				//enableSalidaConsola(false);
				
				
			}
			private static boolean permitirLog(String modulo,String level)
			{
				boolean permitirModulo=false;
				boolean permitirLevel=false;
				
				if(modulo.equalsIgnoreCase(MOD_ANLS_LEXICO)) permitirModulo=ENABLE_LOGGIN_MOD_ANLS_LEXICO;
				else if(modulo.equalsIgnoreCase(MOD_EJECUTOR)) permitirModulo=ENABLE_LOGGIN_MOD_EJECUTOR;
				else if(modulo.equalsIgnoreCase(MOD_WEBCRAWLER)) permitirModulo=ENABLE_LOGGIN_MOD_WEBCRAWLER;
				else if(modulo.equalsIgnoreCase(MOD_UTILS)) permitirModulo=ENABLE_LOGGIN_MOD_UTILS;
				else if(modulo.equalsIgnoreCase(MOD_CARGA_DOCUMENTOS)) permitirModulo=ENABLE_LOGGIN_MOD_CARGA_DOCUMENTOS;
				else if(modulo.equalsIgnoreCase(MOD_GUI)) permitirModulo=ENABLE_LOGGIN_MOD_GUI;
				else  if(modulo.equalsIgnoreCase(MOD_CARGA_JAR)) permitirModulo=ENABLE_LOGGIN_MOD_CARGA_JAR;
				else if(modulo.equalsIgnoreCase(MOD_CONFIGURACION)) permitirModulo=ENABLE_LOGGIN_MOD_CONFIGURACION;
				else if(modulo.equalsIgnoreCase(MOD_ALGORITMO)) permitirModulo=ENABLE_LOGGIN_MOD_ALGORITMO;
				
				if(level.equalsIgnoreCase(LEVEL_ERROR)) permitirLevel=ENABLE_LOGGIN_LEVEL_ERROR;
				else if(level.equalsIgnoreCase(LEVEL_MSG)) permitirLevel=ENABLE_LOGGIN_LEVEL_MSG;
				else if(level.equalsIgnoreCase(LEVEL_SUCESS)) permitirLevel=ENABLE_LOGGIN_LEVEL_SUCESS;
				else if(level.equalsIgnoreCase(LEVEL_WARNING)) permitirLevel=ENABLE_LOGGIN_LEVEL_WARNING;
				else if(level.equalsIgnoreCase(LEVEL_LOAD)) permitirLevel=ENABLE_LOGGIN_LEVEL_LOAD;
				
				
				return permitirModulo&&permitirLevel;
			}
	
}
